		    /*
		    	Insert new user
		    */
			function newUserBtnHandler( ) {
				var newUserDialog = new dijit.Dialog( {
						id		: "userInsertDialog",
						title	: "Insert New User",
						href	: "/FinalProject/registerUser.jsp"
					});
				newUserDialog.onLoad = function () {
					dijit.byId( "dlgResetButton").onClick = resetUserDialog( "userInsertDialog");
					dijit.byId( "dlgSubmitButton").onClick = submitUserDialog( "userInsertDialog", dijit.byId("waitingGrid").store);
					dojo.connect( dijit.byId( "dlgSubmitButton"), "onClick", function(){
						if ( newUserDialog.getValues( ).activityStatus == null ||
								newUserDialog.getValues( ).activityStatus.length == 0) {
							dijit.byId( "waitingGrid").store.newItem( newUserDialog.getValues( ));
						}
						else {
							if ( new Date( dijit.byId( "userInsertDialog").getValues().activityStatus) > new Date( ))
								dijit.byId( "activeGrid").store.newItem( newUserDialog.getValues( ));
							else
								dijit.byId( "disabledGrid").store.newItem( newUserDialog.getValues( ));
						}
					});
				};
				newUserDialog.onCancel = function( e) {
					newUserDialog.destroyRecursive( );
				};				
				newUserDialog.refresh( );
				newUserDialog.show( );
			}

			/*
				Update user details
			*/
			function userUpdateHandler( grid, currentStore) {
				return function( e) {
					var updateDialog = new dijit.Dialog( {
								id		: "userUpdateDialog",
								title 	: "Edit User Information",
								href 	: "/FinalProject/templates/users/updateUser.jsp"
					});
					updateDialog.refresh( ); 
					var dataItem = grid.getItem( e.rowIndex);
					updateDialog.onLoad = function( e) {
						dijit.byId( "dlgResetButton").onClick = resetUserDialog( "userUpdateDialog");
						dijit.byId( "dlgSubmitButton").onClick = submitUserDialog( "userUpdateDialog", currentStore);
						updateDialog.setValues( {
							userLogin 		: dataItem.userLogin,
							firstName		: dataItem.firstName,
							lastName  		: dataItem.lastName,
							phoneNumber  	: dataItem.phoneNumber,
							activityStatus 	: new Date( dataItem.activityStatus[0]),
							role			: dataItem.role
						});
					};
					updateDialog.onCancel = function( e) {
						updateDialog.destroyRecursive( );
					};
					updateDialog.show( );
				};			  	
			};

			/*
				Just need to clean form and close dialog window
			*/
			function resetUserDialog( dialogName) {
				return function( ) {
					dojo.byId( "userForm").reset( );
					dijit.byId( dialogName).hide( );
					dijit.byId( dialogName).destroyRecursive( );
				}
			}
			
			/*
				Send AJAX request to store user details within datbase
			*/
			function submitUserDialog( dialogName, currentStore) {
				return function() {
					if ( dialogName == "userInsertDialog") {
						if ( !dijit.byId( "userLogin").value ||
							  !dijit.byId( "userPassword").value ||
							  !dijit.byId( "userPasswordApproved").value ||
							  !dijit.byId( "firstName").value ||
							  dijit.byId( "userLogin").value.length == 0 ||
							  dijit.byId( "userPassword").value.length == 0 ||
							  dijit.byId( "userPasswordApproved").value.length == 0 ||
							  dijit.byId( "firstName").value.length == 0 ) {
					        dijit.byId('toaster').setContent('You need to fill all marked fields.', 'fatal');
					        dijit.byId('toaster').show();							
					        return;
						}
						if ( dijit.byId( "userPasswordApproved").value != dijit.byId( "userPassword").value) {
					        dijit.byId('toaster').setContent('Password was not matched. Please type again.', 'fatal');
					        dijit.byId('toaster').show();
					        return;
						}
					}
					dijit.byId( "userLogin").attr( "disabled", "false");
					dojo.xhrPost( {
						form : "userForm",
						content : { userLogin : dijit.byId( "userLogin").value},
						load : function( data, ioArgs) {
							if ( data == "ERROR") {
						        dijit.byId('toaster').setContent('Duplicate user login. Please enter another one.', 'fatal');
						        dijit.byId('toaster').show();							
								return;
							}
					        dijit.byId('toaster').setContent('User was sucessefuly added.', 'fatal');
					        dijit.byId('toaster').show();	
					        currentStore.fetch({
					                query: { userLogin: dijit.byId( "userLogin").value },
					                onComplete: function(items, result){
					                        dojo.forEach(items, function(item){
					                        	if (dijit.byId( dialogName).getValues().activityStatus == null ||
					                        			dijit.byId( dialogName).getValues().activityStatus.length == 0 ) {
							                        for( var param in dijit.byId( dialogName).getValues()) {
							                        	currentStore.setValue(item, param, dijit.byId( dialogName).getValues()[param]);
							                        }
					                        	} else {
						                        	if ( new Date( dijit.byId( dialogName).getValues().activityStatus) > new Date( ))
						                        		dijit.byId( "activeGrid").store.newItem( dojo.mixin( dijit.byId( dialogName).getValues(), {userLogin : dijit.byId( "userLogin").value}));
						                        	else
						                        		dijit.byId( "disabledGrid").store.newItem( dojo.mixin( dijit.byId( dialogName).getValues(), {userLogin : dijit.byId( "userLogin").value}));
						                        	currentStore.deleteItem( item);
					                        	}
					                        });
					                }
		                        });					
							resetUserDialog( dialogName)( );						
						},
						error : function( err, ioArgs) {
					        dijit.byId('toaster').setContent('Duplicate user login. Please enter another one', 'fatal');
					        dijit.byId('toaster').show();							
							return;
						}
					});
				}
			}

			function filterUserGrid( control) {
				var filterControl = control.id;
				var gridId = userGridState.currentGrid.id;
				var gridQuery = userGridState.filterQueryMap[gridId];
				var result = { 
					userLoginFilter : function( ) {
						gridQuery.userLogin = control.attr( "value") + "*";
					},
					userFirstNameFilter : function( ) {
						gridQuery.firstName = control.attr( "value") + "*";
					},
					userLastNameFilter : function( ) {
						gridQuery.lastName = control.attr( "value") + "*";
					},
					userPhoneNumberFilter : function( ) {
						gridQuery.phoneNumber = control.attr( "value") + "*";
					}
				}[filterControl];
				if ( result) {
					result( );
				}
				userGridState.currentGrid.setQuery( gridQuery);
			} 

			function filterProjectGrid( control) {
				var gridId = projectGridState.currentGrid.id;
				var gridQuery = projectGridState.filterQueryMap[gridId];
				var result = {
						projectNameFilter 	: function( ) {
							gridQuery.projectName = control.attr( "value") + "*";
						},
						projectDescriptionFilter : function( ) {
							gridQuery.projectDescription = control.attr( "value") + "*";
						}
				}[control.id];
				if ( result) {
					result( );
				}
				projectGridState.currentGrid.setQuery( gridQuery);
			}

			function resetProjectGridFilter( ) {
				var gridId = projectGridState.currentGrid.id;
				projectGridState.currentGrid.setQuery( projectGridState.filterQueryMap[gridId] = {projectName:"*"});
				dijit.byId( "projectNameFilter").attr( "value", "");
				dijit.byId( "projectDescriptionFilter").attr( "value", "");
			}
			
			function resetUserGridFilter( ) {
				var gridId 		= userGridState.currentGrid.id;
				userGridState.filterQueryMap[gridId] = { userLogin : "*"};
				userGridState.currentGrid.setQuery( { userLogin : "*"});
				dijit.byId( "userLoginFilter").attr( "value", "");
				dijit.byId( "userFirstNameFilter").attr( "value", "");
				dijit.byId( "userLastNameFilter").attr( "value", "");
				dijit.byId( "userPhoneNumberFilter").attr( "value", ""); 
			}

			function showUsersList( grid){
				userGridState.currentGrid = dijit.byId( grid);
				var gridQuery = userGridState.filterQueryMap[grid];
				if ( gridQuery.userLogin) {
					dijit.byId( "userLoginFilter").attr( "value", gridQuery.userLogin.replace( "\*", ""));
				}
				if ( gridQuery.firstName) {
					dijit.byId( "userFirstNameFilter").attr( "value", gridQuery.firstName.replace( "\*", ""));
				}
				if ( gridQuery.lastName) {
					dijit.byId( "userLastNameFilter").attr( "value", gridQuery.lastName.replace( "\*", ""));
				}
				if ( gridQuery.phoneNumber) {
					dijit.byId( "userPhoneNumberFilter").attr( "value", gridQuery.phoneNumber.replace( "\*", ""));
				} 
			}